package com.itic.appbase.applications.sys.common.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.applications.sys.code.persistence.model.CodeDetail;
import com.itic.appbase.applications.sys.code.vo.CodeDetailVo;
import com.itic.appbase.applications.sys.common.service.InnerCommonService;
import com.itic.appbase.applications.sys.org.persistence.model.Org;
import com.itic.appbase.applications.sys.region.persistence.model.Region;
import com.itic.appbase.applications.sys.region.persistence.model.RegionExample;
import com.itic.appbase.applications.sys.region.service.RegionService;
import com.itic.appbase.applications.sys.resource.persistence.model.Resource;
import com.itic.appbase.applications.sys.role.persistence.model.Role;
import com.itic.appbase.applications.sys.role.persistence.model.RoleExample;
import com.itic.appbase.applications.sys.role.service.RoleService;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.InitConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.ValueHelper;

@Controller
@RequestMapping(value = "/sys/inner")
public class InnerCommonController
        extends BaseController {

    @Autowired
    private InnerCommonService innerCommonSerivce;
    @Autowired
    private RegionService regionService;

    @Autowired
    private RoleService roleService;

    /**
     * 查找某节点下的树形行政区划 没有id默认全查
     * 
     * @param id
     * @param pId
     * @return
     */
    @RequestMapping(value = "/regionTree")
    @ResponseBody
    public Map<String, Object> getRegionTree(String id, String pId) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Region region = new Region();
        if (StringHelper.isBlank(id) && StringHelper.isBlank(pId)) {// id 和 pId
                                                                    // 全空全查
            region.setpId(InitConst.P_ID);
        } else {
            if (StringHelper.isNotBlank(id)) {// id有值优先id
                region.setId(id);
            } else {// id没值使用pId
                region.setpId(pId);
            }
        }
        region.setIsEffective(DBConst.TRUE);
        List<Region> data = innerCommonSerivce.selectRegionList(region);
        resultMap.put(IticConst.DATA, data);
        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        return resultMap;
    }

    /**
     * 按id查找行政区划
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/regionInfo")
    @ResponseBody
    public Map<String, Object> getRegion(String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = ResponseCode.PARAM_INVALID.getCode();
        if (StringHelper.isNotBlank(id)) {
            resultMap.put(IticConst.DATA, regionService.get(id));
            result = ResponseCode.SUCCESS.getCode();
        }
        resultMap.put(IticConst.RESULT, result);
        return resultMap;
    }

    /**
     * 获取代码表信息 type :代码表类型，如T_DM_SEX filterCode :需要过滤的代码
     * 
     * @param codeDetailVo
     * @return
     */
    @RequestMapping(value = "/getCodeDetail")
    @ResponseBody
    public List<CodeDetailVo> getCodeDetail(CodeDetailVo codeDetailVo) {
        // 声明返回对象
        List<CodeDetailVo> codeDetailVoList = new ArrayList<CodeDetailVo>();
        // 获取数据
        List<CodeDetail> codeDetailList = innerCommonSerivce.getCodeDetail(codeDetailVo);

        if (null != codeDetailList && !codeDetailList.isEmpty()) {
            // 封装数据
            for (CodeDetail codeDetail : codeDetailList) {
                CodeDetailVo vo = new CodeDetailVo();
                vo.setId(codeDetail.getId());
                vo.setPid(codeDetail.getParentId());
                vo.setCode(codeDetail.getCode());
                vo.setName(codeDetail.getName());
                codeDetailVoList.add(vo);
            }
        }
        return codeDetailVoList;
    }

    /**
     * 根据组织机构code，pCode，clazz获取组织机构树
     * 
     * @param org
     * @return
     */
    @RequestMapping(value = "/getOrgTree")
    @ResponseBody
    public Map<String, Object> getOrgTree(Org org) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Org queryOrg = new Org();
        queryOrg.setIsEffective(DBConst.TRUE);
        // 封装查询组织机构code
        if (null == org || StringHelper.isBlank(org.getCode()) || StringHelper.isBlank(org.getpCode())) {
            queryOrg.setpCode(InitConst.P_ID);
        } else {
            if (StringHelper.isNotBlank(org.getCode())) { // code有值,优先code
                queryOrg.setCode(org.getCode());
            } else {
                queryOrg.setpCode(org.getpCode()); // code没值，pcode
            }

            // 组织机构类别
            if (StringHelper.isNoneBlank(org.getClazz())) {
                queryOrg.setClazz(org.getClazz());
            }
        }

        List<Org> list = innerCommonSerivce.getOrgTree(queryOrg);

        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        resultMap.put(IticConst.DATA, list);
        return resultMap;
    }

    /**
     * 获取角色树
     * 
     * @param role
     * @return
     */
    @RequestMapping(value = "/getRoleTree")
    @ResponseBody
    public Map<String, Object> getRoleTree(Role role) {
        Map<String, Object> dataResult = new HashMap<String, Object>();
        RoleExample roleExample = new RoleExample();
        // 封装查询条件
        com.itic.appbase.applications.sys.role.persistence.model.RoleExample.Criteria criteria = roleExample
                .createCriteria().andIsEffectiveEqualTo(DBConst.TRUE);
        if (null != role && StringHelper.isNotBlank(role.getName())) {// 角色名称
            criteria.andNameLike("%" + role.getName() + "%");
        }
        // 查询数据
        List<Role> roleList = roleService.list(roleExample);
        dataResult.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        dataResult.put(IticConst.DATA, roleList);
        return dataResult;
    }

    /**
     * 查找某节点下的资源 没有id默认全查
     * 
     * @param id
     * @param pId
     * @return
     */
    @RequestMapping(value = "/resourceTree")
    @ResponseBody
    public List<Resource> getResourceTree(String id, String pId) {
        Resource resource = new Resource();
        if (StringHelper.isBlank(id) && StringHelper.isBlank(pId)) {
            resource.setpId(InitConst.P_ID);
        } else {
            if (StringHelper.isNotBlank(id)) {
                resource.setId(id);
            } else {
                resource.setpId(pId);
            }
        }
        resource.setIsEffective(DBConst.TRUE);
        return innerCommonSerivce.resourceTree(resource);
    }

    /**
     * 获得某节点下的行政区划集合
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/getSubRegion")
    @ResponseBody
    public List<Map<String, Object>> getSubRegion(String id) {
        RegionExample example = new RegionExample();
        if (StringHelper.isBlank(id)) {
            example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andIdEqualTo(InitConst.ID);
        } else {
            example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andIdEqualTo(id);
        }
        List<Region> regions = regionService.list(example);
        // 返回属性处理
        return ValueHelper.tranList2MapList(regions, new String[] { "sName",
                "isEffective", "createTime", "orgCode", "updateTime", "createUser" });
    }
}
